package com.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.common.ResponseResult;
import com.shop.entity.Manage;
import com.shop.entity.vo.ManageRoleVo;
import com.shop.mapper.manageMapper;
import com.shop.service.manageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
//import java.util.Date;
import java.util.Date;
import java.util.List;

@Transactional
@Service
public class manageServiceImpl implements manageService {
    @Autowired
    private manageMapper manageMapper;

    @Override
    public List<Manage> selectAll() {
        return manageMapper.selectList(null);
    }

    @Override
    public Page<Manage> selectPage(Integer pagesize, Integer pagenum) {
        Page<Manage> page = new Page<>(pagesize,pagenum);
        page=manageMapper.selectPage(page,null);
        return page;
    }

    @Override
    public Page<ManageRoleVo> selectVo(@RequestParam(required = false) Integer pagesize,
                                       @RequestParam(required = false) Integer pagenum,
                                       @RequestParam(required = false) String query) {
        Page<ManageRoleVo> page = new Page<>(pagesize,pagenum);
        if (query==null){
            query="";
        }
        page = manageMapper.selectManageVo(page,query);
        return page;
    }

    @Override
    public ResponseResult addManage(Manage manage) {
        ResponseResult result = new ResponseResult();
        QueryWrapper<Manage> wrapper=new QueryWrapper<>();
        wrapper.eq("mg_name",manage.getUsername());
        Manage manage1= manageMapper.selectOne(wrapper);
        if (manage1!=null){
            result.setINTERNALSERVERERROR("已经存在");
            return result;
        }
        if (manage.getUsername()==null||manage.getPassword()==null){
            result.setBADREQUEST();
            return result;
        }
        long nowTime = System.currentTimeMillis()/1000;
        Integer nowTimeInt = new Long(nowTime).intValue();
        manage.setMg_time(nowTimeInt);
        int insert = manageMapper.insert(manage);
        result.setCREATED("添加成功",manage);
        return result;
    }

    @Override
    public ResponseResult deleteManage(Integer id) {
        ResponseResult result = new ResponseResult();
        int insert = manageMapper.deleteById(id);
        if (insert==1){
            result.setDELETED();
        }else {
            result.setINTERNALSERVERERROR();
        }
        return result;
    }

    @Override
    public ResponseResult updateManage(Integer uId, boolean type) {
        ResponseResult result = new ResponseResult();
        Manage manage = manageMapper.selectById(uId);
        manage.setType(type);
        int i = manageMapper.updateById(manage);
        if (i==1){
            result.setSuccess();
        }else {
            result.setSuccess("修改错误");
        }
        return result;
    }

    @Override
    public ResponseResult selectOne(Integer uId) {
        ResponseResult result = new ResponseResult();
        QueryWrapper<Manage> wrapper = new QueryWrapper();
        wrapper.select(Manage.class,info->!info.getColumn().equals("mg_pwd"));
        wrapper.eq("mg_id",uId);
        Manage manage = manageMapper.selectOne(wrapper);
        if (manage==null){
            result.setSuccess("没有查询到相关内容");
        }else {
            result.setSuccess("查询成功",manage);
        }
        return result;
    }

    @Override
    public ResponseResult updateOne(Integer id, Manage manage) {
        ResponseResult result = new ResponseResult();
        manage.setId(id);
        int insert = manageMapper.updateById(manage);
        if (insert==1){
            result.setCREATED("修改成功",manage);
        }else {
            result.setINTERNALSERVERERROR();
        }
        return result;
    }

    @Override
    public ResponseResult updateRole(Integer id, Integer rid) {
        ResponseResult result =new ResponseResult();
        Manage manage=manageMapper.selectById(id);
        manage.setRole_id(rid);
        int insert = manageMapper.updateById(manage);
        if (insert==1){
            result.setCREATED("修改成功",manage);
        }else {
            result.setINTERNALSERVERERROR();
        }
        return result;
    }
}
